//
//  YBHomePackageCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/5.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBHomePackageCell: UICollectionViewCell {
    
    override var isSelected: Bool {
        didSet {
            bgImgView.image = UIImage(named: isSelected ? "icon_home_package_bg_select" : "icon_home_package_bg_normal")
            leftImgView.image = UIImage(named: isSelected ? "icon_home_left_select" : "icon_home_left_normal")
            rightImgView.image = UIImage(named: isSelected ? "icon_home_right_select" : "icon_home_right_normal")
            titleLabel.textColor = isSelected ? kRGBColor(184, 169, 136) : ColorDef.gray130
            desLabel.textColor = isSelected ? kRGBColor(184, 169, 136) : ColorDef.gray130
            infoTitleLabel.textColor = isSelected ? kRGBColor(184, 169, 136) : ColorDef.gray153
            infoLabel.textColor = isSelected ? kRGBColor(184, 169, 136) : ColorDef.gray153
            super.isSelected = isSelected
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(bgImgView)
        bgImgView.addSubviews([titleLabel, leftImgView, rightImgView, recommendImgView, desLabel, infoTitleLabel, infoLabel])
        
        bgImgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        recommendImgView.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.equalTo(28)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(18).priority(.high)
        }
        
        leftImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(6.5, 10))
            make.centerY.equalTo(titleLabel)
            make.right.equalTo(titleLabel.snp.left).offset(-11.5)
        }
        
        rightImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(6.5, 10))
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(11.5)
        }
        
        desLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(7.5)
        }
        
        infoTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(23)
            make.top.equalTo(desLabel.snp.bottom).offset(27)
        }
        
        infoLabel.snp.makeConstraints { make in
            make.centerY.equalTo(infoTitleLabel)
            make.left.equalTo(infoTitleLabel.snp.right).offset(6)
            make.right.equalTo(-12)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var bgImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_home_package_bg_normal"))
        iv.isUserInteractionEnabled = true
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var recommendImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_home_recommend"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var leftImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_home_left_normal"))
        return iv
    }()
    
    private lazy var rightImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_home_right_normal"))
        return iv
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "计算型", font: .systemFont(ofSize: 15, weight: .bold), bgColor: .clear, textColor: ColorDef.gray130, align: .center)
    
    private lazy var desLabel = self.bk_addLabel(text: "三维建模、图形图像", font: .systemFont(ofSize: 11), bgColor: .clear, textColor: ColorDef.gray130, align: .center)
    
    private lazy var infoTitleLabel: UILabel = {
        let label = self.bk_addLabel(font: .systemFont(ofSize: 11), bgColor: .clear, textColor: ColorDef.gray153)
        label.attributedText = NSMutableAttributedString("显      卡：") { $0
            .lineSpacing(8)
        }.addText("\n当前排队：") { $0
            .lineSpacing(8)
        }.addText("\n预计等待：") { $0
            .lineSpacing(8)
        }
        return label
    }()
    
    private lazy var infoLabel: UILabel = {
        let label = self.bk_addLabel(font: .systemFont(ofSize: 11), bgColor: .clear, textColor: ColorDef.gray153)
        label.attributedText = NSMutableAttributedString("RTX 4060") { $0
            .lineSpacing(8)
        }.addText("\n55人") { $0
            .lineSpacing(8)
        }.addText("\n55min") { $0
            .lineSpacing(8)
        }
        return label
    }()
    
}

// MARK: - Public
extension YBHomePackageCell {
    
    func updateCell(by model: YBOrderInfoVosModel, isRecommend: Bool) {
        recommendImgView.isHidden = !isRecommend
        titleLabel.text = model.type == 6 ? "大型游戏" : "休闲游戏"
        desLabel.text = model.type == 6 ? "大型游戏、动态渲染" : "三维建模、普通游戏"
        
        var text = model.type == 6 ? "RTX 4060" : "RTX A5000"
        infoLabel.attributedText = NSMutableAttributedString(text) { $0
            .lineSpacing(8)
        }.addText("\n\(model.lineUpNum)人") { $0
            .lineSpacing(8)
        }.addText("\n\(model.waitTime)") { $0
            .lineSpacing(8)
        }
    }
    
}
